var api = require('../../utils/api.js')
var app = getApp()
Page({
    data: {
        code:'',
        openid:'',
        nickname:''
    },

    onLoad() {
  
    },
    onShow(){
        //调用登录接口
        var that=this;
        wx.login({
            success: function (ret) {
            console.log(ret);
            wx.getUserInfo({
                success: function () {
                    api.post({
                        url: 'wxapp/getopenid',
                        data: {'code':ret.code},
                        success: data => {
                            that.setData({
                                openid:data.data.openid
                            })
                        }
                    })
                }
            })

            that.setData({
                code:ret.code
            })
            }
        })

    },

    getvalue(ret){
        console.log(ret);

        this.setData({
            nickname:ret.detail.value
        })
    },

    getnikname(ret){
        console.log(ret.detail);

        var that=this;
        var openid=that.data.openid;
        var agentid=wx.getStorageSync('agentid');
        var nickname=this.data.nickname;

        api.post({
            url: 'wxapp/wxrequest',
            data: {'openid':openid,nickname:nickname,agentid:agentid},
            success: data => {
                var info=data.data;
                console.log(data.msg) 
    

                wx.showToast({
                  title: data.msg,
                    icon:'none'
                })
                    //注册成功!
                if(data.code==1){

                    wx.setStorageSync('user', info.userinfo)
                    wx.setStorageSync('token', info.userinfo.token)


                    wx.switchTab({
                        url: '/pages/my/my',
                    })
                }
            

            }
        })
   

    },
    goindex(){
        wx.switchTab({
          url: '/pages/index/index',
        })
    },
    getPhoneNumber: function(e){
        console.log(e.detail);
        var code=this.data.code;
        api.post({
            url: 'wxapp/getmobile',
            data: {'code':code},
            success: data => {
                console.log(data)
            }
        })
    },
})
